﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AcountingService.Messages.Voucher;
using BusinessObjects.BussinessVoucher;
using DataObjects;
using DataObjects.StandartInterfaces.Voucher;

namespace AcountingService.CommonClass
{
    public class ItemTransactioCommonFunction
    {
        private static readonly IItemTransactionDao ItemTransactionDao = DataAccess.ItemTransactionDao;

        private static readonly IItemTransactionDetailDao ItemTransactionDetailDao = DataAccess.ItemTransactionDetailDao;
        /// <summary>
        /// Gets the list journal entry account .
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        public IList<JournalEntryAccount> GetListJournalEntryAccount(ItemTransactionRequest request)
        {
            var itemTransaction = request.ItemTransaction;
            // var obJourentryAccount = new JournalEntryAccount();
            IList<JournalEntryAccount> obListJournalEntryAccount = new List<JournalEntryAccount>();
            foreach (var itemTransactionDetail in request.ItemTransactionDetails)
            {
                var iItemTransactionDetailId = itemTransactionDetail.ItemTransactionDetailId;
                var obJourentryAccount = new JournalEntryAccount
                {
                    RefId = itemTransaction.RefId,
                    RefTypeId = itemTransaction.RefTypeId,
                    RefNo = itemTransaction.RefNo,
                    RefDate = itemTransaction.RefDate,
                  
                    RefDetailId = iItemTransactionDetailId,
                    AccountNumber = itemTransactionDetail.AccountNumber,
                    CorrespondingAccountNumber = itemTransactionDetail.CorrespondingAccountNumber,
                    AmountOc = itemTransactionDetail.AmountOc,
                    Description = itemTransactionDetail.Description,
                    VoucherTypeId = itemTransactionDetail.VoucherTypeId,

                    UnitId = itemTransaction.UnitId
                };
                // Chèn dữ liệu phần Details

                obListJournalEntryAccount.Add(obJourentryAccount);
                ////AccountNumberVat
                //if (!string.IsNullOrEmpty(itemTransactionDetail.AccountNumberVat) || !itemTransactionDetail.AccountNumberVat.Equals("")) 
                //{
                //    var obJourentryAccount2 = new JournalEntryAccount
                //    {
                //        RefId = itemTransaction.RefId,
                //        RefTypeId = itemTransaction.RefTypeId,
                //        RefNo = itemTransaction.RefNo,
                //        RefDate = itemTransaction.RefDate,

                //        RefDetailId = iItemTransactionDetailId,
                //        AccountNumber = itemTransactionDetail.AccountNumber,
                //        CorrespondingAccountNumber = itemTransactionDetail.AccountNumberVat,
                //        AmountOc = itemTransactionDetail.AmountVat,
                //        Description = itemTransactionDetail.Description,
                //        VoucherTypeId = itemTransactionDetail.VoucherTypeId,

                //        UnitId = itemTransaction.UnitId
                //    };
                //    obListJournalEntryAccount.Add(obJourentryAccount2);
              //  }
              
             
            }


            return obListJournalEntryAccount;
        }

        /// <summary>
        /// Gets the list journal entry account .
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        public IList<JournalEntryAccount> GetListJournalEntryAccountForTax(ItemTransactionRequest request)
        {
            var itemTransaction = request.ItemTransaction;
            // var obJourentryAccount = new JournalEntryAccount();
            IList<JournalEntryAccount> obListJournalEntryAccountForTax = new List<JournalEntryAccount>();
            foreach (var itemTransactionDetail in request.ItemTransactionDetails)
            {
                var iItemTransactionDetailId = itemTransactionDetail.ItemTransactionDetailId;
                //var obJourentryAccount = new JournalEntryAccount
                //{
                //    RefId = itemTransaction.RefId,
                //    RefTypeId = itemTransaction.RefTypeId,
                //    RefNo = itemTransaction.RefNo,
                //    RefDate = itemTransaction.RefDate,

                //    RefDetailId = iItemTransactionDetailId,
                //    AccountNumber = itemTransactionDetail.AccountNumber,
                //    CorrespondingAccountNumber = itemTransactionDetail.CorrespondingAccountNumber,
                //    AmountOc = itemTransactionDetail.AmountOc,
                //    Description = itemTransactionDetail.Description,
                //    VoucherTypeId = itemTransactionDetail.VoucherTypeId,

                //    UnitId = itemTransaction.UnitId
                //};
                //// Chèn dữ liệu phần Details

                //obListJournalEntryAccount.Add(obJourentryAccount);
                //AccountNumberVat
              //  if (!string.IsNullOrEmpty(itemTransactionDetail.AccountNumberVat) || !itemTransactionDetail.AccountNumberVat.Equals(""))
                 if (itemTransactionDetail.AccountNumberVat !=null)
                {

                    var obJourentryAccount2 = new JournalEntryAccount
                    {
                        RefId = itemTransaction.RefId,
                        RefTypeId = itemTransaction.RefTypeId,
                        RefNo = itemTransaction.RefNo,
                        RefDate = itemTransaction.RefDate,

                        RefDetailId = iItemTransactionDetailId,
                        
                        
                        AmountOc = itemTransactionDetail.AmountVat,
                        Description = itemTransactionDetail.Description,
                        VoucherTypeId = itemTransactionDetail.VoucherTypeId,

                        UnitId = itemTransaction.UnitId
                    };
                    if (itemTransaction.RefTypeId==400)
                    {
                        obJourentryAccount2.AccountNumber = itemTransactionDetail.AccountNumberVat;
                        obJourentryAccount2.CorrespondingAccountNumber = itemTransactionDetail.CorrespondingAccountNumber;
                    }
                    else
                    {
                        obJourentryAccount2.AccountNumber = itemTransactionDetail.CorrespondingAccountNumber;
                        obJourentryAccount2.CorrespondingAccountNumber = itemTransactionDetail.AccountNumberVat;
                    }
                    obListJournalEntryAccountForTax.Add(obJourentryAccount2);
                }


            }


            return obListJournalEntryAccountForTax;
        }

        /// <summary>
        /// Gets the list journal entry account .
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        public IList<TaxTrans> GetListTaxTrans(ItemTransactionRequest request)
        {
            var itemTransaction = request.ItemTransaction;
            // var obJourentryAccount = new JournalEntryAccount();
            IList<TaxTrans> obListTaxTrans = new List<TaxTrans>();
            foreach (var itemTransactionDetail in request.ItemTransactionDetails)
            {
                var iItemTransactionDetailId = itemTransactionDetail.ItemTransactionDetailId;
                var obJourentryAccount = new TaxTrans
                {
                    RefId = itemTransaction.RefId,
                    RefTypeId = itemTransaction.RefTypeId,
                    RefNo = itemTransaction.RefNo,
                    RefDate = itemTransaction.RefDate,

                    RefDetailId = iItemTransactionDetailId,
                    AccountNumber = itemTransactionDetail.AccountNumber,
                    CorrespondingAccountNumber = itemTransactionDetail.CorrespondingAccountNumber,
                    AmountOc = itemTransactionDetail.AmountOc,
                    Description = itemTransactionDetail.Description,
                    VoucherTypeId = itemTransactionDetail.VoucherTypeId,

                    UnitId = itemTransaction.UnitId,

                    AccountNumberVat = itemTransactionDetail.AccountNumberVat,
                    AmountVat = itemTransactionDetail.AmountVat,
                    TaxRate = itemTransactionDetail.TaxRate,
                };
                // Chèn dữ liệu phần Details

                obListTaxTrans.Add(obJourentryAccount);
            }


            return obListTaxTrans;
        }

        /// <summary>
        /// Gets the list account balance.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        public IList<AccountBalance> GetListAccountBalance(ItemTransactionRequest request)
        {
            var itemTransaction = request.ItemTransaction;
            IList<AccountBalance> obListAccountBalance = new List<AccountBalance>();
            foreach (var itemTransactionDetail in request.ItemTransactionDetails)
            {
                var obAccountBalance = new AccountBalance
                {
                    BalanceDate = itemTransaction.RefDate,
                  
                    AccountNumber = itemTransactionDetail.AccountNumber,
                    MovementDebitAmountOC = itemTransactionDetail.AmountOc,
                   
                    MovementCreditAmountOC = 0,
                    UnitId = itemTransaction.UnitId,
                 //   MovementCreditAmountExchange = 0
                };
                //Dòng tài khoản nợ
                obListAccountBalance.Add(obAccountBalance);
                // Dòng tài khoản có
                var obAccountBalance1 = new AccountBalance
                {
                    BalanceDate = itemTransaction.RefDate,
                  //  CurrencyCode = itemTransaction.CurrencyCode,
                  //  ExchangeRate = itemTransaction.ExchangeRate,
                    AccountNumber = itemTransactionDetail.CorrespondingAccountNumber,
                    MovementCreditAmountOC = itemTransactionDetail.AmountOc,
                  //  MovementCreditAmountExchange = itemTransactionDetail.AmountExchange,
                    //BudgetSourceCode = itemTransactionDetail.BudgetSourceCode,
                    //BudgetItemCode = itemTransactionDetail.BudgetItemCode,
                    //ProjectId = itemTransactionDetail.ProjectId,
                    MovementDebitAmountOC = 0,
                    UnitId = itemTransaction.UnitId,
                //    MovementDebitAmountExchange = 0
                };

                obListAccountBalance.Add(obAccountBalance1);
            }

            return obListAccountBalance;
        }

        /// <summary>
        /// Gets the list account balance older.
        /// </summary>
        /// <param name="refId">The reference identifier.</param>
        /// <returns></returns>
        public IList<AccountBalance> GetListAccountBalanceOlder(long refId)
        {
            var itemTransaction = ItemTransactionDao.GetItemTransaction(refId);
            itemTransaction.ItemTransactionDetails = ItemTransactionDetailDao.GetItemTransactionDetailsByItemTransaction(refId);
            IList<AccountBalance> obListAccountBalance = new List<AccountBalance>();
            foreach (var itemTransactionDetail in itemTransaction.ItemTransactionDetails)
            {
                var obAccountBalance = new AccountBalance
                {
                    BalanceDate = itemTransaction.RefDate,
                  //  CurrencyCode = itemTransaction.CurrencyCode,
                  //  ExchangeRate = itemTransaction.ExchangeRate,
                    AccountNumber = itemTransactionDetail.AccountNumber,
                    MovementDebitAmountOC = itemTransactionDetail.AmountOc,
                    //MovementDebitAmountExchange = itemTransactionDetail.AmountExchange,
                    //BudgetSourceCode = itemTransactionDetail.BudgetSourceCode,
                    //BudgetItemCode = itemTransactionDetail.BudgetItemCode,
                    //ProjectId = itemTransactionDetail.ProjectId,
                    MovementCreditAmountOC = 0,
                    UnitId = itemTransaction.UnitId,
                  //  MovementCreditAmountExchange = 0
                };
                //Dòng tài khoản nợ
                obListAccountBalance.Add(obAccountBalance);
                // Dòng tài khoản có
                var obAccountBalance1 = new AccountBalance
                {
                    BalanceDate = itemTransaction.RefDate,
                    //CurrencyCode = itemTransaction.CurrencyCode,
                    //ExchangeRate = itemTransaction.ExchangeRate,
                    AccountNumber = itemTransactionDetail.CorrespondingAccountNumber,
                    MovementCreditAmountOC = itemTransactionDetail.AmountOc,
                    //MovementCreditAmountExchange = itemTransactionDetail.AmountExchange,
                    //BudgetSourceCode = itemTransactionDetail.BudgetSourceCode,
                    //BudgetItemCode = itemTransactionDetail.BudgetItemCode,
                    //ProjectId = itemTransactionDetail.ProjectId,
                    MovementDebitAmountOC = 0,
                    UnitId = itemTransaction.UnitId,
                 //   MovementDebitAmountExchange = 0
                };

                obListAccountBalance.Add(obAccountBalance1);
            }

            return obListAccountBalance;
        }
    }
}